package assignment7;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;

import javax.swing.JOptionPane;

/**
 * Class to manage data
 * 
 * @author Terry Wasserleben CS204-21370 Prof. Jeannette Myers Due: 11/21/2011
 */
public class DataManager {
	
	private String p_home_unx = "/home/";
	private String p_home_win = "C:\\";
	private String path = "";
	private String p_sep = "";
	private String homepath = "twasser\\Documents\\School\\2011\\09 Fall\\CS204-21370\\workspace\\assignment7\\src\\";
	
	private File file = null;
	private RandomAccessFile part;
	private RandomAccessFile frds;
	
	private static String[] edgeTemp;
	
	private FriendGraph fg = new FriendGraph();
	
	/**
	 * Constructor to call various methods to get data
	 */
	public DataManager() {
		system_check();
		load();
	}
	
	/**
	 * method that gets executed by default within the Constructor so no need
	 * for public visibility
	 */
	private void load() {
		File participants = new File(path + p_sep + "Participants.txt");
		File friends = new File(path + p_sep + "Friends.txt");
		try {
			part = new RandomAccessFile(participants, "rw");
			Scanner in = null;
			int bytes = 0;
			String tempV;
			String[] temp = new String[3];
			for (int i = 0; bytes < part.length(); i++) {
				tempV = part.readLine();
				bytes += tempV.length();
				in = new Scanner(tempV);
				in.useDelimiter(":");
				for (int j = 0; in.hasNext(); j++) {
					temp[j] = in.next();
				}
				if (!(temp.length == 0)) {
					addProfile(temp[0], temp[1], temp[2]);
				}
			}
		}
		catch (IOException e1) {
			e1.printStackTrace();
		}
		try {
			frds = new RandomAccessFile(friends, "rw");
			int edgeRecs = (int) frds.length();
			edgeTemp = new String[edgeRecs];
			for (int i = 0; i < frds.length(); i++) {
				frds.seek(i);
				edgeTemp[i] = frds.readLine();
			}
		}
		catch (IOException e2) {
			e2.printStackTrace();
		}
	}
	
	/**
	 * builds the path to the file for either a Unix file system or a Windows
	 * file system
	 */
	private void system_check() {
		if (path_check(p_home_unx)) {
			p_sep = "/";
			path = p_home_unx;
			Scanner sep = new Scanner(homepath);
			sep.useDelimiter("\\\\");
			while (sep.hasNext()) {
				path += sep.next() + p_sep;
			}
		} else if (path_check(p_home_win)) {
			p_sep = "\\";
			path = p_home_win + "Users" + p_sep;
			path += homepath;
		} else {
			JOptionPane
					.showMessageDialog(
							null,
							"System Error:\n\nThis is neither a Unix or Windows based computer\n\nOR\n\nThere is something blocking access to the filesystem.\nPlease check Java settings or have you MAC/*nix/PC repaired soon!");
			system_check();
		}
	}
	
	/**
	 * method to find the home path of the system which will be indicative of a
	 * Unix based OS or a Windows based OS
	 * 
	 * @param home String passed in which will be a / or a \\ to find the
	 * @return boolean returned (true or false)
	 */
	private boolean path_check(String home) {
		file = new File(home);
		if (file.exists()) {
			return true;
		}
		return false;
	}
	
	/**
	 * method to write to the file that contains the vertices
	 * 
	 * @param vertex
	 * @throws IOException
	 */
	public void writeVertex(String[] vertex) throws IOException {
		for (String e : vertex) {
			part.writeChars(e);
		}
	}
	
	/**
	 * method to write to the file that contains the edges
	 * 
	 * @param edge
	 * @throws IOException
	 */
	public void writeEdge(String[] edge) throws IOException {
		for (String e : edge) {
			frds.writeChars(e);
		}
	}
	
	/**
	 * method to return Profiles in a String[] for easier data handling
	 * 
	 * @return String[] returned of ALL profiles listed within the Vertex Set
	 */
	public String[] getProfile() {
		// TODO Auto-generated method stub
		String[] temp = null;
		int i = 0;
		for (Vertex e : fg.vertexSet()) {
			temp[i] = e.getVertex();
			i++;
		}
		return temp;
	}
	
	/**
	 * method to format the contents of the Vertex String for use within the
	 * profile box
	 * 
	 * @param temp String passed in to be formatted
	 * @return
	 */
	public String toStringProfile(String temp) {
		Scanner in = new Scanner(temp);
		in.useDelimiter(":");
		return in.next() + " " + in.next() + " of " + in.next();
	}
	
	/**
	 * method to use regular strings to place into a Vertex object and then
	 * added to the data structure
	 * 
	 * @param fName First Name passed
	 * @param lName Last Name passed
	 * @param hTown Hometown passed
	 */
	public void addProfile(String fName, String lName, String hTown) {
		Vertex vertex = new Vertex(fName, lName, hTown);
		fg.addVertex(vertex);
	}
}
